package org.esti.controller;

import org.esti.controller.exceptions.PermissionDeniedToDropAdminAccount;
import org.esti.model.Level;
import org.esti.model.User;
import org.esti.model.UserRoles;
import org.esti.model.exceptions.UserAlreadyExistException;
import org.esti.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Date;

/**
 * @author ALoginov <a href="mailto:Artem.Loginov@exigenservices.com">Artem Loginov</a>
 * @since 10/14/11
 */

/**
 * this controller is used for authorization and managing admin account
 */
@Controller
public class AuthController {

    private static final String ADMIN_LOGIN = "admin";
    private static final Date ADMIN_BIRTHDAY = new Date(70, 1, 1);

    @Value("${security.dropAdminToken}")
    private String securityToken;

    @Autowired
    private UserService userService;

    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    public void setSecurityToken(String securityToken) {
        this.securityToken = securityToken;
    }

    public String getSecurityToken() {
        return securityToken;
    }

    /**
     * show login page
     * @return login view name
     */
    @RequestMapping("/login")
    public String showLoginPage() {
        return "login";
    }

    /**
     * this method is used to drop admin account and create empty with predefined login and password
     * @param requestSecurityToken token which will be validated to drop admin account
     * @return view name
     */
    @RequestMapping("/create_admin_account/{requestSecurityToken}")
    public String dropAdminAccount(
            @PathVariable String requestSecurityToken
    ) {
        if (requestSecurityToken.equals(securityToken)) {
            registerAdminUser();
            return "redirect:/";
        }
        throw new PermissionDeniedToDropAdminAccount("invalid data token");
    }

    private void registerAdminUser() {
        User admin = new User();
        updateUserFileds(admin);
        try {
            userService.createUser(admin);
        } catch (UserAlreadyExistException ex) {
            User user = userService.getUserByLogin(ADMIN_LOGIN);
            user = updateUserFileds(user);
            userService.updateUser(user);
        }
    }

    private User updateUserFileds(User admin) {
        admin.setLogin(ADMIN_LOGIN);
        admin.setPassword("445566");
        admin.setName(ADMIN_LOGIN);
        admin.setMiddleName(ADMIN_LOGIN);
        admin.setSurname(ADMIN_LOGIN);
        admin.setBirthday(ADMIN_BIRTHDAY);
        admin.setEmail("admin@example.com");
        admin.setRole(UserRoles.ROLE_ADMIN);
        admin.setLevel(new Level());
        admin.setLocked(false);
        return admin;
    }

}